package Servelts;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.DAO;
import daoFactory.DaoFactory;
import daoImpls.UserDAOImpl;
import vo.Role;
import vo.User;
@WebServlet("/LoginOrRegisterServlet")
public class LoginOrRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException, IOException {
	        doPost(request,response);
		   }
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			String method = request.getParameter("method");// 获取JSP界面的method参数来判断执行什么方法
			String url = "error.jsp";
			if (method.equals("login")){
				login(request, response);// 登录方法
			}else if(method.equals("register")){
				Register(request, response);// 注册方法
			}
			if(method.equals("logined")){
				response.getWriter().println("<script type='text/javascript'>  alert('请先登录！'); location.href='Login_Register.jsp'; </script>");
			}
			if(method.equals("loginpage")){
				request.getRequestDispatcher("Login_Register.jsp.jsp").forward(request, response);
			}
			//忘记密码
			if(method.equals("forge")){
				url="forgetPwd.jsp";
				request.getRequestDispatcher("forgetPwd.jsp").forward(request, response);
			}
			
		}
		//登陆
		protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
			HttpSession session = request.getSession();
			PrintWriter out=response.getWriter();
			try {
				String username = request.getParameter("l_username");
				String password = request.getParameter("l_password");// 获取用户名密码
				if(username.equals("")|password.equals("")|username==null|password==null){
					out.println("<script type='text/javascript'>  alert('您登录界面输入框有未输入值！！'); location.href='Login_Register.jsp'; </script>");
				}else{
					try{
						DAO<Object> userdao = new UserDAOImpl();
						User user = (User)userdao.getU(username);
						if(user.getPwd().equals(password)){
							System.out.println(username +"登录...");
							session.setAttribute("role",user.getRole().getRid());
							session.setAttribute("userid", user.getUid());
				    		session.setAttribute("username", username);
				    		response.sendRedirect("jump?status=shopindex");
						}else{
							System.out.println("登录失败");
							out.println("<script type='text/javascript'>  alert('登录失败，账号或者密码错误'); location.href='Login_Register.jsp'; </script>");
						}
					}catch(NullPointerException e){
						out.println("<script type='text/javascript'>  alert('该账号未注册哦！！'); location.href='Login_Register.jsp'; </script>");
					}
				}
			}catch (Exception e) {
	            System.out.println("找不到驱动类:"+e);
	        }
		}
		/*
		 * 用户注册
		 */
		protected void Register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			PrintWriter out=response.getWriter();
			try {
				String status = request.getParameter("satus");
				String username=request.getParameter("r_username");
				String password1=request.getParameter("r_password1");
				String password2=request.getParameter("r_password2");
				Role role = DaoFactory.getRoleFactory().get("2");
				
				String url = "Login_Register.jsp";
				if(username.equals("")|password1.equals("")|password2.equals("")|username==null|password1==null|password2==null){
					out.println("<script type='text/javascript'>  alert('您注册界面输入框有未输入值！！'); location.href='Login_Register.jsp'; </script>");
				}else{
					if(password1.equals(password2)){
						User user_judge = DaoFactory.getUserFactory().getU(username);
						if(user_judge==null){
							User user = new User();
							user.setUname(username);
							user.setPwd(password1);
							user.setRole(role);
							user.setSex("男");
							if(DaoFactory.getUserFactory().add(user)){
								out.println("<script type='text/javascript'>  alert('注册成功！!自动为您跳转到登陆界面'); location.href=' " + url + "'; </script>");
							}else{
								System.out.println("注册失败");
							}
						}else{
							out.println("<script type='text/javascript'>  alert('账号已经存在，请换个~'); location.href='Login_Register.jsp'; </script>");
						}
					}
					else{
						out.println("<script type='text/javascript'>  alert('注册失败，两次密码不一致'); location.href='Login_Register.jsp'; </script>");
					}
				}
				}catch (Exception e) {
					System.out.println("找不到驱动类:"+e);
				}
	}

}
